﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KC.Presentation.Mv8CMS
{
	public partial class ResistCategories : Form
	{
		private string csMv8 = System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString;

		public ResistCategories() { InitializeComponent(); }
		private void ResistCategories_Load(object sender, EventArgs e) { LoadCategories(); }

		private void LoadCategories()
		{
			dgCategories.DataSource = KC.DataAccess.Global.Global.CallSql_Query(csMv8, "select ID, Name, Description from resist..Categories order by Name");
			dgCategories.Columns["ID"].Visible = false;
			dgCategories.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
			dgCategories.Columns["Description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			dgCategories.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
			clear();
		}

		private void dgCategories_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			dgCategories.ClearSelection();
			dgCategories.Rows[e.RowIndex].Selected = true;
			DataRowView dr = (DataRowView)dgCategories.Rows[e.RowIndex].DataBoundItem;
			txtID.Text = dr["ID"].ToString();
			txtName.Text = dr["Name"].ToString();
			txtDesc.Text = dr["Description"].ToString();
			txtFriendlyName.Text = dr["FriendlyName"].ToString();
			txtName.Focus();
		}

		private void btnClear_Click(object sender, EventArgs e) { clear(); }

		private void clear()
		{
			dgCategories.ClearSelection();
			txtID.Text = "";
			txtName.Text = "";
			txtFriendlyName.Text = "";
			txtDesc.Text = "";
			txtName.Focus();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			KC.DataAccess.Global.Global.CallSql_Query(csMv8, txtID.Text.Length == 0
				? "insert into resist..Categories (name, Description,Friendlyname) values ('" + txtName.Text.Replace("'", "''") + "', '" + txtDesc.Text.Replace("'", "''") + "','"+txtFriendlyName.Text.Replace("'","''")+"')"
				: "update resist..Categories set Name='" + txtName.Text.Replace("'", "''") + "', Description='" + txtDesc.Text.Replace("'", "''") + "', Friendlyname='"+txtFriendlyName.Text.Replace("'","''")+"' where ID=" + txtID.Text);
			LoadCategories();
			txtName.Focus();
		}

		private void btnFriendly_Click(object sender, EventArgs e)
		{
			BuildFriendlyName();
			txtDesc.Focus();
		}
		private void BuildFriendlyName()
		{
			string FriendlyName = System.Web.HttpUtility.UrlEncode(txtName.Text);
			int iteration = 0;
			while (KC.DataAccess.Global.Global.CallSql_Query(csMv8, "select count(ID) from resist..Categories where FriendlyName='" + FriendlyName.Replace("'", "''") + "' " + (txtID.Text.Length > 0 ? " and ID <> " + txtID.Text : "")).Rows[0][0].ToString() != "0") {
				iteration++;
				FriendlyName = System.Web.HttpUtility.UrlEncode(txtName.Text) + iteration.ToString(); }
			txtFriendlyName.Text = FriendlyName;
		}

		private void txtName_Leave(object sender, EventArgs e)
		{
			if (txtFriendlyName.Text.Length == 0) BuildFriendlyName();
			txtDesc.Focus();
		}
	}
}
